import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:playtruly/common/models/index.dart';

class DeleteFriendDialog extends StatelessWidget {
  final String userName;
  final String userAvatar;
  final String userLocation;
  final String userAge;
  final String userGender;
  final String userSignature;
  final int userLikes;
  final int userFollowers;
  final int userPoints; // 🔥 新增：用户积分，用于计算等级
  final List<String> userTags;
  final RxList<ActivityData> userActivities; // 真实的活动数据
  final bool isLoading;
  final bool isDeletingFriend;
  final VoidCallback onDeleteConfirm;
  final VoidCallback onClearHistory;

  const DeleteFriendDialog({
    super.key,
    required this.userName,
    required this.userAvatar,
    required this.userLocation,
    required this.userAge,
    required this.userGender,
    required this.userSignature,
    required this.userLikes,
    required this.userFollowers,
    required this.userPoints, // 🔥 新增：用户积分参数
    required this.userTags,
    required this.userActivities, // 活动数据参数
    required this.isLoading,
    required this.isDeletingFriend,
    required this.onDeleteConfirm,
    required this.onClearHistory,
  });

  // 🔥 根据积分计算等级名称（与用户主页保持一致）
  String getLevelNameFromPoints(int points) {
    final level = calculateLevelFromPoints(points);
    switch (level) {
      case 0:
        return '追随者';
      case 1:
        return '守护者';
      case 2:
        return '创造者';
      case 3:
        return '真神';
      case 4:
        return '一言九鼎';
      default:
        return '追随者';
    }
  }
  
  // 🔥 根据积分计算等级数字
  int calculateLevelFromPoints(int points) {
    if (points >= 1200) {
      return 4; // 一言九鼎
    } else if (points >= 600) {
      return 3; // 真神
    } else if (points >= 300) {
      return 2; // 创造者
    } else if (points >= 100) {
      return 1; // 守护者
    } else {
      return 0; // 追随者
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: const BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(20),
          topRight: Radius.circular(20),
        ),
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          // 标题
          Container(
            padding: const EdgeInsets.symmetric(vertical: 20),
            child: const Text(
              '好友信息',
              style: TextStyle(
                fontSize: 18,
                fontWeight: FontWeight.bold,
                color: Colors.black,
              ),
            ),
          ),
          
          // 用户信息区域（新样式）
          Container(
            margin: const EdgeInsets.symmetric(horizontal: 20),
            child: Column(
              children: [
                // 头像和用户名居中显示
                Column(
                  children: [
                    // 头像（移除等级标签）
                    ClipRRect(
                      borderRadius: BorderRadius.circular(40),
                      child: CachedNetworkImage(
                        imageUrl: userAvatar,
                        width: 80,
                        height: 80,
                        fit: BoxFit.cover,
                        errorWidget: (context, url, error) {
                          return Container(
                            width: 80,
                            height: 80,
                            color: Colors.grey[300],
                            child: const Icon(Icons.person, color: Colors.grey, size: 40),
                          );
                        },
                      ),
                    ),
                    const SizedBox(height: 12),
                    // 用户名和等级居中显示
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        // 用户名
                        Text(
                          userName,
                          style: const TextStyle(
                            fontSize: 20,
                            fontWeight: FontWeight.bold,
                            color: Colors.black,
                          ),
                        ),
                        const SizedBox(width: 8),
                        // 🔥 等级标签（基于积分计算）
                        Container(
                          padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
                          decoration: BoxDecoration(
                            color: const Color(0xFFE33641),
                            borderRadius: BorderRadius.circular(12),
                          ),
                          child: Text(
                            getLevelNameFromPoints(userPoints),
                            style: const TextStyle(
                              color: Colors.white,
                              fontSize: 12,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                        ),
                      ],
                    ),
                    const SizedBox(height: 8),
                    // 性别、位置、年龄、个性签名居中显示（使用真实数据）
                    if (isLoading)
                      Container(
                        padding: EdgeInsets.symmetric(vertical: 10),
                        child: CircularProgressIndicator(
                          strokeWidth: 2,
                          valueColor: AlwaysStoppedAnimation<Color>(Color(0xFFE33641)),
                        ),
                      )
                    else
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          // 根据性别显示不同图标
                          Icon(
                            userGender == '男' ? Icons.male : 
                            userGender == '女' ? Icons.female : Icons.person,
                            color: userGender == '男' ? Colors.blue : 
                                   userGender == '女' ? Colors.pink : Colors.grey,
                            size: 16,
                          ),
                          const SizedBox(width: 4),
                          Text(
                            '$userLocation',
                            style: TextStyle(
                              fontSize: 14,
                              color: Colors.grey[600],
                            ),
                          ),
                          const SizedBox(width: 4),
                          Text(
                            '|',
                            style: TextStyle(
                              fontSize: 14,
                              color: Colors.grey[600],
                            ),
                          ),
                          const SizedBox(width: 4),
                          Text(
                            '$userAge岁',
                            style: TextStyle(
                              fontSize: 14,
                              color: Colors.grey[600],
                            ),
                          ),
                          const SizedBox(width: 4),
                          Text(
                            '|',
                            style: TextStyle(
                              fontSize: 14,
                              color: Colors.grey[600],
                            ),
                          ),
                          const SizedBox(width: 4),
                          Flexible(
                            child: Text(
                              userSignature,
                              style: TextStyle(
                                fontSize: 14,
                                color: Colors.grey[600],
                              ),
                              overflow: TextOverflow.ellipsis,
                            ),
                          ),
                        ],
                      ),
                  ],
                ),
                const SizedBox(height: 24),
                
                // 最近动态标题
                const Align(
                  alignment: Alignment.centerLeft,
                  child: Text(
                    '最近动态',
                    style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                      color: Colors.black,
                    ),
                  ),
                ),
                const SizedBox(height: 12),
                
                // 动态内容区域（使用真实数据）
                Obx(() {
                  // 检查是否有活动数据
                  if (userActivities.isEmpty) {
                    // 没有活动时显示提示信息
                    return Container(
                      width: double.infinity,
                      height: 120,
                      padding: const EdgeInsets.all(16),
                      decoration: BoxDecoration(
                        color: Colors.grey[50],
                        borderRadius: BorderRadius.circular(12),
                      ),
                      child: Center(
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Icon(
                              Icons.article_outlined,
                              color: Colors.grey[400],
                              size: 40,
                            ),
                            const SizedBox(height: 8),
                            Text(
                              '暂无动态',
                              style: TextStyle(
                                fontSize: 14,
                                color: Colors.grey[600],
                              ),
                            ),
                          ],
                        ),
                      ),
                    );
                  }
                  
                  // 有活动时显示第一条活动
                  final latestActivity = userActivities.first;
                  final hasImage = latestActivity.activityImages.isNotEmpty;
                  
                  return Container(
                    width: double.infinity,
                    padding: const EdgeInsets.all(16),
                    decoration: BoxDecoration(
                      color: Colors.grey[50],
                      borderRadius: BorderRadius.circular(12),
                    ),
                    child: Row(
                      children: [
                        // 左侧图片（如果有的话）
                        if (hasImage)
                          ClipRRect(
                            borderRadius: BorderRadius.circular(8),
                            child: CachedNetworkImage(
                              imageUrl: latestActivity.activityImages.first,
                              width: 120,
                              height: 120,
                              fit: BoxFit.cover,
                              errorWidget: (context, url, error) {
                                return Container(
                                  width: 120,
                                  height: 120,
                                  color: Colors.grey[300],
                                  child: Icon(
                                    Icons.image,
                                    color: Colors.grey[500],
                                    size: 40,
                                  ),
                                );
                              },
                            ),
                          )
                        else
                          // 没有图片时显示占位图标
                          Container(
                            width: 120,
                            height: 120,
                            decoration: BoxDecoration(
                              color: Colors.grey[300],
                              borderRadius: BorderRadius.circular(8),
                            ),
                            child: Icon(
                              Icons.article_outlined,
                              color: Colors.grey[500],
                              size: 40,
                            ),
                          ),
                        
                        const SizedBox(width: 12),
                        
                        // 右侧文字内容
                        Expanded(
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              // 活动标题
                              Text(
                                latestActivity.activityTitle.isNotEmpty ? latestActivity.activityTitle : '无标题',
                                style: const TextStyle(
                                  fontSize: 16,
                                  fontWeight: FontWeight.bold,
                                  color: Colors.black,
                                ),
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                              ),
                              const SizedBox(height: 8),
                              // 活动内容
                              Text(
                                latestActivity.activityDetails.isNotEmpty ? latestActivity.activityDetails : '无内容',
                                style: TextStyle(
                                  fontSize: 13,
                                  color: Colors.grey[700],
                                  height: 1.4,
                                ),
                                maxLines: 3,
                                overflow: TextOverflow.ellipsis,
                              ),
                              const SizedBox(height: 6),
                              // 活动位置
                              if (latestActivity.activityLocation.isNotEmpty)
                                Row(
                                  children: [
                                    Icon(
                                      Icons.location_on_outlined,
                                      size: 12,
                                      color: Colors.grey[500],
                                    ),
                                    const SizedBox(width: 2),
                                    Expanded(
                                      child: Text(
                                        latestActivity.activityLocation,
                                        style: TextStyle(
                                          fontSize: 11,
                                          color: Colors.grey[500],
                                        ),
                                        maxLines: 1,
                                        overflow: TextOverflow.ellipsis,
                                      ),
                                    ),
                                  ],
                                ),
                              const SizedBox(height: 4),
                              // 发布时间
                              if (latestActivity.activityPublishTime.isNotEmpty)
                                Text(
                                  latestActivity.activityPublishTime,
                                  style: TextStyle(
                                    fontSize: 11,
                                    color: Colors.grey[500],
                                  ),
                                ),
                            ],
                          ),
                        ),
                      ],
                    ),
                  );
                }),
              ],
            ),
          ),
          
          const SizedBox(height: 20),
          
          // 按钮区域
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 20),
            child: Column(
              children: [
                // 清空记录按钮
                SizedBox(
                  width: double.infinity,
                  height: 50,
                  child: ElevatedButton(
                    onPressed: isDeletingFriend ? null : () {
                      Get.back();
                      onClearHistory(); // 调用真实的清空记录方法
                    },
                    style: ElevatedButton.styleFrom(
                      backgroundColor: Colors.grey[300],
                      foregroundColor: Colors.black,
                      elevation: 0,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(25),
                      ),
                    ),
                    child: const Text(
                      '清空记录',
                      style: TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                  ),
                ),
                const SizedBox(height: 12),
                // 删除好友按钮（带加载状态）
                SizedBox(
                  width: double.infinity,
                  height: 50,
                  child: ElevatedButton(
                    onPressed: isDeletingFriend ? null : () {
                      Get.back();
                      onDeleteConfirm(); // 调用真实的删除好友方法
                    },
                    style: ElevatedButton.styleFrom(
                      backgroundColor: Colors.black,
                      foregroundColor: Colors.white,
                      elevation: 0,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(25),
                      ),
                    ),
                    child: isDeletingFriend 
                        ? Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              SizedBox(
                                width: 16,
                                height: 16,
                                child: CircularProgressIndicator(
                                  strokeWidth: 2,
                                  valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
                                ),
                              ),
                              SizedBox(width: 8),
                              Text(
                                '删除中...',
                                style: TextStyle(
                                  fontSize: 16,
                                  fontWeight: FontWeight.w500,
                                ),
                              ),
                            ],
                          )
                        : const Text(
                            '删除好友',
                            style: TextStyle(
                              fontSize: 16,
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                  ),
                ),
              ],
            ),
          ),
          
          const SizedBox(height: 30),
        ],
      ),
    );
  }
}
